package yl.hs.bmipfull.service.material.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.material.IRawMaterialInfoDao;
import yl.hs.bmipfull.pojo.material.RawMaterialInfoEntity;
import yl.hs.bmipfull.service.material.IRawMaterialInfoService;
import yl.hs.bmipfull.utils.DataState;
import yl.hs.bmipfull.utils.HsApp;
import yl.hs.bmipfull.utils.SearchFilterEngine;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作用:  作用:  原材信息服务层接口实现 <br/>
 * 创建时间: 2021-05-18 11:10 <br/>
 *
 * @author 吴高丰
 */
@Service
public class RawMaterialInfoServiceImpl extends PurchaseBaseService implements IRawMaterialInfoService {
    @Autowired
    IRawMaterialInfoDao dao;

    @Override
    public List<RawMaterialInfoEntity> queryListByRawCateSn(String rawCateSn) {
        return dao.queryListByRawCateSn(rawCateSn);
    }

    @Override
    public List<RawMaterialInfoEntity> queryList(PageQuery pageQuery) throws Exception {
        pageQuery.checkSortSides(RawMaterialInfoEntity.class);
        pageQuery.getQuery().put("unitSNArray", getCompanySNList());
        var s = dao.queryList(pageQuery.getQuery());
        return s;
    }

    @Override
    public PageResult<RawMaterialInfoEntity> queryList(PageQuery pageQuery, int rowNum, int page) throws Exception {
        var result = new PageResult<RawMaterialInfoEntity>(rowNum);
        Map<String, Object> queryParam = pageQuery.getQuery();
        var filters = SearchFilterEngine.generateQueryList(pageQuery.getFilters(),RawMaterialInfoEntity.class);
        result.setRecords(dao.queryCountByPage(queryParam,filters));
        result.setPage(page);
        pageQuery.checkSortSides(RawMaterialInfoEntity.class);
        queryParam.put("unitSNArray", getCompanySNList());
        queryParam.put("querySkip", (page - 1) * rowNum);
        queryParam.put("pageSize", rowNum);
        queryParam.put("sortSide", String.join(",", pageQuery.getSortSides()));
        result.setData(dao.queryListByPage(queryParam,filters));
        return result;
    }

    @Override
    public RawMaterialInfoEntity findBySn(String sn) {
        return dao.findBySN(sn);
    }

    @Override
    @Transactional
    public Integer saveRawMaterialInfoEntity(Map<String, Object> model, String sn) throws Exception {
        Integer result = 0;
        RawMaterialInfoEntity entity = new RawMaterialInfoEntity();
        if (Tl.isEmpty(sn)) {
            //执行保存
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            entity.setSn(queryNewSN(RawMaterialInfoEntity.class));
            entity.setOrganizationCode(getDepartSN());
            if (entity.getState() == null) {
                entity.setState(DataState.CREATED.code);
            }
            result = dao.insertNew(entity);
            if (result > 0) {
                writeLog(ActionType.CREATE, entity.getSn(), "插入原材信息", Tl.toJson(entity), RawMaterialInfoEntity.class);
            }

        } else {
            entity = findBySn(sn);
            //执行修改
            var changelog = Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            entity.setOrganizationCode(getDepartSN());
            result = dao.update(entity);
            if (result > 0) {
                writeLog(ActionType.UPDATE, sn, changelog, Tl.toJson(entity), RawMaterialInfoEntity.class);
            }
        }
        if (result > 0) {
            saveGS(
                    entity.getSn(),
                    entity.getRmName(),
                    String.join(",",
                            entity.getRmName(),
                            entity.getRmCateName(),
                            entity.getRmSpec(),
                            entity.getBrand(),
                            entity.getRemark()
                    ),
                    entity,
                    HsApp.getCurrent() != null ? HsApp.getCurrent().getUserSN() : null,
                    entity.getOrganizationCode()
            );
        }
        return result;
    }

    @Override
    @Transactional
    public Integer delete(String sn) {
        var snArray = sn.split(",");
        var result = dao.changeStateBySn(snArray, DataState.DELETED.code);
        if (result > 0) {
            writeLog(ActionType.DELETE, snArray, RawMaterialInfoEntity.class);
            deleteGs(snArray);
        }
        return result;
    }

    @Override
    public List<RawMaterialInfoEntity> findByRawCateSn(String rawCateSn) {
        return dao.queryListByRawCateSn(rawCateSn);
    }

    @Override
    public List<RawMaterialInfoEntity> getAllInfo() {
        return dao.getAllInfo();
    }

    @Override
    public List<RawMaterialInfoEntity> queryByRmName(String rmName) {
        return dao.queryByRmName(rmName);
    }

    @Override
    public Integer changeStateBySn(String[] snArray, short state) {
        int result=dao.changeStateBySn(snArray,state);
        if (result > 0) {
            forWriteLog(snArray,state);
        }
        return result;
    }

}
